package com.peng.leetcode.other;

/**
 * MyAtoi
 *
 * @author: lupeng6
 * @create: 2021/1/1 21:19
 */
public class MyAtoi {

    public static void main(String[] args) {
        System.out.println(new MyAtoi().myAtoi( "-91283472332"));
    }

    public int myAtoi(String s) {
        if (s == null || s.length() <= 0) {
            return 0;
        }
        s = s.trim();
        long num = 0;
        boolean flag = false;
        int index = 0;
        char c1 = s.charAt(index);
        if (c1 == '-') {
            flag = true;
            index++;
        } else if (c1 < '0' || c1 > '9'){
            return 0;
        }

        while (index < s.length()) {
            char c = s.charAt(index);
            if (c < '0' || c > '9') {
                break;
            }
            num += Integer.valueOf(c + "");
            if (index + 1 < s.length() && s.charAt(index + 1) >= '0' && s.charAt(index + 1) <= '9') {
                num *= 10;
            }
            index++;
        }
        if (flag) {
            return (int) -num;
        }
        return (int) num;
    }
}
